package com.huawei.hmsdemo10086.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.huawei.hmsdemo10086.POJO.Result;
import com.huawei.hmsdemo10086.POJO.User;
import com.huawei.hmsdemo10086.R;

import java.io.IOException;
import java.lang.reflect.Type;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class BindActivity extends AppCompatActivity {

    TextView phone;
    TextView password;
    Button login_button;
    Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind);
        phone = (TextView)findViewById(R.id.phone);
        password = (TextView)findViewById(R.id.password);
        login_button = (Button)findViewById(R.id.login_button);
        intent = this.getIntent();
        Button register_button = (Button)findViewById(R.id.register);

        register_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent_jump = new Intent(BindActivity.this,RegesitActivity.class);
                intent_jump.putExtra("token",intent.getStringExtra("token"));
                System.out.println(intent.getStringExtra("token"));
                startActivity(intent_jump);
            }
        });

        login_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                check();
            }
        });
    }

    public void bind(int userid)
    {
        String url = "http://10.0.2.2:8443/api/bindUser";
        OkHttpClient mOkHttpClient = new OkHttpClient();
        System.out.println(intent.getStringExtra("token"));
        String json = "{\n" +
                "  \"userid\": \"" + userid + "\",\n" +
                "  \"token\": \"" + intent.getStringExtra("token") + "\"\n" +
                "}";

        Request request = new Request.Builder().url(url).post(RequestBody.create(MediaType.parse("application/json"),json)).build();

        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("测试", e+"");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

            }

        });

    }


    public void check()
    {
        String url = "http://10.0.2.2:8443/api/login";
        OkHttpClient mOkHttpClient = new OkHttpClient();

        String json = "{\n" +
                "  \"phone\": \"" + phone.getText().toString() + "\",\n" +
                "  \"password\": \"" + password.getText().toString() + "\"\n" +
                "}";

        Request request = new Request.Builder().url(url).post(RequestBody.create(MediaType.parse("application/json"),json)).build();

        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("测试", e+"");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Gson gson = new Gson();
                String rep = response.body().string();
                System.out.println(rep);
                Type type = new TypeToken<Result<User>>(){}.getType();
                Result<User> result = gson.fromJson(rep,type);
                if(result.getCode() == 200)
                {
                    //System.out.println(result.getData().getUserid());
                    bind(result.getData().getUserid());
                    //Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(BindActivity.this,MainActivity.class);
                    startActivity(intent);
                }
                else
                {
                    // Toast.makeText(LoginActivity.this, "用户名或者密码错误", Toast.LENGTH_SHORT).show();
                }
            }

        });
    }



}